package com.glory.gloryquickui.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.entity.OptionBean
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import com.glory.gloryquickui.widget.cascade_view.ColumnAdapter
import com.glory.gloryquickui.widget.cascade_view.ColumnEntity
import com.glory.gloryquickui.widget.cascade_view.DialogFromBottom
import kotlin.collections.ArrayList

/**
 *Created:YJ
 *创建时间：2021/4/19
 *Describe:
 */
class CascadeViewController(context: Context?,  val viewModel: QuickUIViewModel) : ViewController<FieldBean?>(context) {

    private var rvRecycle: RecyclerView? = null
    private var mAdapter: ColumnAdapter? = null
    private var dialogFromBottom: DialogFromBottom? = null
    private var dialogContent: View? = null
    private var ivDropDownSelect: ImageView? = null

    private var tvCascadeName: TextView? = null
    private var tvCascadeSelectValue: TextView? = null
    private var flCascadeLayout: FrameLayout? = null

    override fun resLayoutId(): Int {
        return R.layout.layout_cascadeview
    }

    override fun onCreatedView(view: View?) {
        tvCascadeName = view?.findViewById(R.id.tvCascadeName)
        tvCascadeSelectValue = view?.findViewById(R.id.tvCascadeSelectValue)
        flCascadeLayout = view?.findViewById(R.id.flCascadeLayout)
        ivDropDownSelect = view?.findViewById(R.id.ivDropDownSelect);
        flCascadeLayout?.setOnClickListener {
            dialogContent = LayoutInflater.from(context).inflate(R.layout.dialog_cascade, null, false)
            rvRecycle = dialogContent?.findViewById(R.id.rv_Recycle)
            dialogFromBottom = DialogFromBottom(context!!)
            dialogFromBottom?.setContentView(dialogContent!!)
            initData()
        }
    }

    override fun onBindView(data: FieldBean?) {
        tvCascadeName!!.text = data?.config?.label
        tvCascadeSelectValue!!.hint = data!!.placeholder
    }

    fun analysisData(baseEntity:ColumnEntity,childrenArray:ArrayList<OptionBean>,postion:Int){

        var childArray:ArrayList<ColumnEntity> = arrayListOf()
        childrenArray.forEach {
            var entity = ColumnEntity()
            entity.id = it.id
            entity.level = postion
            entity.title = it.label
            if(it.children != null) {
                if (it.children!!.size > 0) {
                    analysisData(entity, it.children!!, postion + 1)
                }
                entity.isDisabled = it.disabled
                childArray.add(entity)
            }
        }
        baseEntity.subLists = childArray
    }

     fun initData(){
        var totalArray:ArrayList<ColumnEntity> = arrayListOf()
        data?.options?.forEach {

            var entity = ColumnEntity()
            entity.id = it.id
            entity.level = 1
            entity.title = it.label
            if(it.children != null){
                if(it.children!!.size >0){
                    analysisData(entity, it.children!!,2)
                }
            entity.isDisabled = it.disabled
            }
            totalArray.add(entity)
        }

        val columnEntityList: MutableList<ColumnEntity> =
            ArrayList<ColumnEntity>()
        val c = ColumnEntity("0", "基类", 0, totalArray)
        columnEntityList.add(c)

        mAdapter = ColumnAdapter(context, columnEntityList)
        rvRecycle?.layoutManager = LinearLayoutManager(
            context,
            LinearLayoutManager.HORIZONTAL,
            false
        )
        rvRecycle?.itemAnimator = null
        rvRecycle?.adapter = mAdapter!!
        mAdapter?.setOnItemClickListener { _, _, _, info, isEnd ->
            if(isEnd){
                dialogFromBottom?.dismiss()
                tvCascadeSelectValue?.text = info.title
                data?.config?.defaultValue = info.title
                val map = viewModel.pageData.value!!.toMutableMap()
                map[data!!.vModel] =info.title
                viewModel.pageData.value = map
                if (data?.clearable!!){
                    ivDropDownSelect?.setImageDrawable(
                        context?.resources?.let {
                            ResourcesCompat.getDrawable(
                                it,
                                R.drawable.clear,
                                null
                            )
                        })

                    ivDropDownSelect?.setOnClickListener{
                        tvCascadeSelectValue?.text = ""
                        ivDropDownSelect?.setImageDrawable(
                            context?.resources?.let {
                                ResourcesCompat.getDrawable(
                                    it,
                                    R.drawable.up_arrow,
                                    null
                                )
                            })
                    }
                }
            }
        }
        dialogFromBottom?.show()
    }

    override fun masterPart(): View? {
        return tvCascadeSelectValue
    }
}